package org.jeecg.modules.demo.mpwlwjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.demo.mpwlwjc.entity.ZwglImageManagement;
import org.jeecg.modules.demo.mpwlwjc.mapper.ZwglImageManagementMapper;
import org.jeecg.modules.demo.mpwlwjc.service.IZwglImageManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @Description: 影像管理
 * @Author: jeecg-boot
 * @Date: 2023-04-13
 * @Version: V1.0
 */
@Service
public class ZwglImageManagementServiceImpl extends ServiceImpl<ZwglImageManagementMapper, ZwglImageManagement> implements IZwglImageManagementService {

    @Autowired
    private ZwglDpsblistServiceImpl zwglDpsblistService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ZwglImageManagementMapper zwglImageManagementMapper;

    @Override
    public String getMpList() {

        long l = System.currentTimeMillis();
        //new日期对象
        Date date = new Date(l);
        //转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(date);
        //调用的api的接口地址

        String apiPath = "https://agro-iot.auto-control.com.cn/api/video-data/list";
        BufferedReader in = null;
        StringBuffer result = null;
        String token = (String) redisUtil.get("mptoken");
        if (Objects.isNull(token)) {
            zwglDpsblistService.getToken();
        }

        try {
            URL url = new URL(apiPath);
            //打开和url之间的连接

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);


            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", token);
            connection.setRequestProperty("Charset", "utf-8");
            connection.connect();
            result = new StringBuffer();
            //读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            String result2 = result.toString(); //返回json字符串
            System.out.println(result2);
            //获取数据
            JSONObject jsonObject = JSON.parseObject(result2);
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            JSONArray jsonArray = jsonObject1.getJSONArray("data");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                ZwglImageManagement zwglImageManagement = new ZwglImageManagement();
                zwglImageManagement.setDeviceId(jsonObject2.getString("deviceId"));
                zwglImageManagement.setDeviceCode(jsonObject2.getString("deviceCode"));
                zwglImageManagement.setDeviceName(jsonObject2.getString("deviceName"));
                zwglImageManagement.setDeviceTypeId(jsonObject2.getString("deviceTypeId"));
                zwglImageManagement.setDeviceTypeName(jsonObject2.getString("deviceTypeName"));
                zwglImageManagement.setStationId(jsonObject2.getString("stationId"));
                zwglImageManagement.setStationName(jsonObject2.getString("stationName"));
                zwglImageManagement.setHlsUrl(jsonObject2.getString("hlsUrl"));
                System.out.println(jsonObject2.getString("online"));
                if (jsonObject2.getString("online") == "true") {
                    zwglImageManagement.setOnline("在线");
                } else {
                    zwglImageManagement.setOnline("离线");
                }
//                zwglImageManagement.setOnline(jsonObject2.getString("online"));
                zwglImageManagementMapper.insert(zwglImageManagement);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

}
